﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using InvestmentIntelligence.Files;
using InvestmentIntelligence.SchedulingService.Infrastructure;
using InvestmentIntelligence.TradingData.DataLoaders.LoadDataProcessor;
using InvestmentIntelligence.TradingData.DataLoaders.PostProcessing;
using InvestmentIntelligence.TradingData.DataLoaders.PostProcessing.TradingDataSecurity;
using InvestmentIntelligence.TradingData.DataLoaders.Rimes;
using InvestmentIntelligence.TradingData.DataProcessing.Models;
using InvestmentIntelligence.TradingData.Handlers.Handlers.CsvHandlers;
using InvestmentIntelligence.TradingData.Handlers.Mus.FieldsConfiguration;
using InvestmentIntelligence.TradingData.TradingDataModels;

namespace InvestmentIntelligence.TradingData.Handlers.Mus
{
    [Export(typeof(IScheduledTaskFactory))]
    [ExportMetadata("TaskName", "LoadMusCsvTrades")]
    public class CsvMusTradesHandlerFactory : FolderBasedHandlerFactoryBase, IScheduledTaskFactory
    {
        private readonly ScheduledTaskParameter _tradesFolder = new StringScheduledTaskParameter(
            "trades-dir", "specifies a directory containing trade data");

        private readonly ScheduledTaskParameter _rimesUser = new StringScheduledTaskParameter(
            "rimes-user", "specifies a rimes user");

        private readonly ScheduledTaskParameter _rimesPassword = new StringScheduledTaskParameter(
            "rimes-password", "specifies a rimes password");

        private readonly ScheduledTaskParameter _fileMask = new StringScheduledTaskParameter(
            "files-mask", "specifies a mask of trades files");

        private readonly ScheduledTaskParameter _dateMask = new StringScheduledTaskParameter(
           "date-mask", "specifies a mask of date field");

        public override IEnumerable<ScheduledTaskParameter> GetParameters()
        {
            yield return _tradesFolder;
            yield return _rimesUser;
            yield return _rimesPassword;
            yield return _fileMask;
            yield return _dateMask;
        }

        public override IScheduledTask CreateTask(CreateTaskParams createTaskParams, IDictionary<ScheduledTaskParameter, string> arguments)
        {
            var tfileds = new TradesFields();
            const TradeComparisonFields tComparisonFields = TradeComparisonFields.CurrencyId |
                                                            TradeComparisonFields.Date |
                                                            TradeComparisonFields.FundBookEntityId |
                                                            TradeComparisonFields.FxRate |
                                                            TradeComparisonFields.OriginalId |
                                                            TradeComparisonFields.Price |
                                                            TradeComparisonFields.Quantity |
                                                            TradeComparisonFields.SecurityId;
            var securityDataLoader = new SecurityDataLoader(_rimesUser.Get(arguments, ""), _rimesPassword.Get(arguments, ""));
            var loadDataProcessor = new LoadDataProcessor<TradingDataSecurityDto>(securityDataLoader, AutomapperConfiguration.Configurations.Rimes.Mappings.TradingDataSecurityDto,
                                                               new List<IDataPostProcessing<TradingDataSecurityDto>>
                                                                {
                                                                    new RimesPostProcessingCurrencyTranslator()
                                                                }, TradingDataSecurityDto.TradingDataSecurityDtoComparer);
            
            var files = FilteredFileReader.GetFiles(_tradesFolder.Get(arguments, ""), _fileMask.Get(arguments, ""), "csv");
            var dateMask = _dateMask.Get(arguments, "M/d/yyyy");

            return new CsvImportTradesHandler(loadDataProcessor, MusBootstrapper.Instance.SecurityResolver, files, tfileds, tComparisonFields, Mappings.TradingDataTradeDto, new[] { dateMask });
        }
    }
}